using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text;

public partial class EventDetails : MyPage
{
    protected string eventText = String.Empty;

    protected string mapDetails = String.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.HttpMethod == "POST" && Request.Form["form_message"].ToString().Length > 0)
        {
            SqlCommand sp = new SqlCommand("cm_postMessage", conn);
            sp.CommandType = CommandType.StoredProcedure;
            sp.Parameters.AddWithValue("@ms_nick", Request.Form["form_name"].ToString());
            sp.Parameters.AddWithValue("@ms_ip", Request.UserHostAddress.ToString());
            sp.Parameters.AddWithValue("@ms_email", Request.Form["form_email"].ToString());
            sp.Parameters.AddWithValue("@ms_text", Request.Form["form_message"].ToString());
            sp.Parameters.AddWithValue("@ms_ev_id", Int32.Parse(Request.QueryString["id"]));
            conn.Open();
            sp.ExecuteNonQuery();
            conn.Close();
        }

        SqlCommand spa = new SqlCommand("cm_getEvent", conn);
        spa.CommandType = CommandType.StoredProcedure;
        spa.Parameters.AddWithValue("@ev_id", Int32.Parse(Request.QueryString["id"]));
        conn.Open();
        SqlDataReader rdr = spa.ExecuteReader();

        if (rdr.Read())
        {
            eventText = String.Format("<div id=\"topnews\"><h2>{0}</h2><div id=\"topnewstime\">{1}</div>{2}<br /></div>", rdr.GetString(5), rdr.GetDateTime(2), rdr.GetString(6));

            mapDetails = String.Format("{0}, {1}, '{2}'", rdr.GetDecimal(3), rdr.GetDecimal(4), rdr.GetString(11));

            Page.Title = rdr.GetString(5);
        }
        else
        {
            Response.Redirect("not_found.aspx");
            Response.End();
            return;
        }
        
        rdr.Close();
        conn.Close();
    }

    protected string EventText()
    {
        return eventText;
    }

    protected string MapDetails()
    {
        return mapDetails;
    }

    protected string BreadCrumb()
    {
        return ">> <a href=\"/England/London.aspx\">London</a> >> <a href=\"#\">Area name</a> >> <a href=\"#\">Suburb name</a>";
    }

    protected string Messages()
    {
        SqlCommand spa = new SqlCommand("cm_getMessages", conn);
        spa.CommandType = CommandType.StoredProcedure;
        spa.Parameters.AddWithValue("@ms_ev_id", Int32.Parse(Request.QueryString["id"]));
        conn.Open();
        SqlDataReader rdr = spa.ExecuteReader();

        if (rdr.HasRows)
        {
            StringBuilder messages = new StringBuilder();
            while (rdr.Read())
            {
                string message = String.Format("<div><i>{0}</i><br /><b>{1}</b> wrote:<br /><br />{2}</div>", rdr.GetDateTime(2).ToString(), rdr.GetString(3), rdr.GetString(5).Replace("\n", "<br />"));
                messages.Append(message);
            }

            return messages.ToString();
        }
        else
        {
            return "<div><i>there are no comments on this article. <b>Leave yours!</b></i></div>";
        }

        rdr.Close();
        conn.Close();
    }

    protected override void SavePageStateToPersistenceMedium(object viewState)
    {

    }
    protected override object LoadPageStateFromPersistenceMedium()
    {
        return null;
    }
}
